<?php
/**
 * author: by wzb
 * time:2019/3/2
 * desc:接口信息
 */
class Dlzorderapi{
	public $token;
	public $baseUrl;
	public $tokenUrl;
	public $userData;
	public $errorMsg;
	public $username;
	public $password;
	public $id_m;
	public $store_name_m;
	public function __construct($shop=null)
    {
            $this->id_m = $shop['id'];
            $this->store_name_m = $shop['store_name'];
            $this->token = $shop['token'];
            $baseUrl_m = $shop['baseUrl'];
            $username_m = $shop['username'];
            $password_m = $shop['password'];
            $this->baseUrl = $baseUrl_m.'/rest/default/V1/';//对接地址 非ssl 
            $this->tokenUrl = 'integration/admin/token';//获取token地址  
            $this->userData = array('username' => "$username_m", 'password' => "$password_m");//API账号密码需要在后台创建
            $this->errorMsg = "";
    }

    public function idm(){
        return $this->id_m;
    }

    public function storenamem(){
        return $this->store_name_m;
    }
    /**
	* 获取产品： salesShipOrderV1 
	* method： post
	* return ： data
	**/
	public function PostTracknumber($orderid,$Data){
	        $Url = "order/$orderid/ship";
	        $ch = curl_init();
	        curl_setopt($ch, CURLOPT_URL, $this->baseUrl . $Url);
	        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
	        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($Data));
	        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
	        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                'Content-Type:application/json',
	            'Accept:application/json',
	            'authorization: Bearer ' . $this->token
	        ));
	        $result = curl_exec($ch);
	        curl_close($ch);
	        $result = json_decode($result, TRUE);
	        return $result;
	}

	/**
	* 获取运单信息： salesShipmentRepositoryV1  
	* method： get
	* return ： data
	**/
	public function GetShippingInfo($ship_entity_id){
	        $catagoryUrl = "shipment/$ship_entity_id";
	        $ch = curl_init();
	        curl_setopt($ch, CURLOPT_URL, $this->baseUrl . $catagoryUrl);
	        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
	        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
	        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
	            'Accept:application/json',
	            'Authorization: Bearer ' . $this->token
	        ));
	        $result = curl_exec($ch);
	        curl_close($ch);
	        $result = json_decode($result, TRUE);//return category array
	        //print_r($result);
	        return $result;

	}
	
	/**
	* 修改运单信息： salesShipmentTrackRepositoryV1
	* method： post
	* return ： data
	**/
	public function EditPostTracknumberinfo($Data){
	        $Url = "shipment/track";
	        $ch = curl_init();
	        curl_setopt($ch, CURLOPT_URL, $this->baseUrl . $Url);
	        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
	        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($Data));
	        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
	        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                'Content-Type:application/json',
                'Accept:application/json',
	            'authorization: Bearer ' . $this->token
	        ));
	        $result = curl_exec($ch);
	        curl_close($ch);
            $result = json_decode($result, TRUE);//return category array
	        return $result;
	}

	/**
	* 获取产品目录： catalogCategoryManagementV1  
	* method： get
	* return ： data
	**/
	public function GetproductCategory(){
	        $catagoryUrl = "categories";
	        $ch = curl_init();
	        curl_setopt($ch, CURLOPT_URL, $this->baseUrl . $catagoryUrl);
	        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
	        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
	        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
	            'Accept:application/json',
	            'Authorization: Bearer ' . $this->token
	        ));
            echo "$this->token";
	        $result = curl_exec($ch);
	        curl_close($ch);
	        $result = json_decode($result, TRUE);//return category array
	       // print_r($result);
	        return $result;

	}
	/**
* 获取产品： catalogProductRepositoryV1 GET GET /V1/products
* method： get
* return ： data
**/
    public function Getproduct($pageSize,$currentPage)
    {
        echo "$pageSize,$currentPage\n";
        $datas = array(
        'searchCriteria[filterGroups][][filters][][field]' => '',
        'searchCriteria[filterGroups][][filters][][value]' => '',
        'searchCriteria[filterGroups][][filters][][conditionType]' => '',
        'searchCriteria[sortOrders][][field]' => '',
        'searchCriteria[sortOrders][][direction]' => '',
        'searchCriteria[pageSize]' => "$pageSize",//每页数量
        'searchCriteria[currentPage]' => "$currentPage"//当前页
        );
        $json_str = $this->sendPost($datas);
        $catagoryUrl = "products?$json_str";
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $this->baseUrl . $catagoryUrl);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'Accept:application/json',
            'Authorization: Bearer ' . $this->token
        ));
        $result = curl_exec($ch);
        curl_close($ch);
        $result = json_decode($result, TRUE);//return category array
        //print_r($result);
        return $result;
    }
/**
* 根据 sku ： catalogProductRepositoryV1 GET /V1/products/{sku} 获取产品信息
* method： get
* return ： data
**/
    public function getproductExtensionAttr($sku)
    {
        $catagoryUrl = "products/$sku";
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $this->baseUrl . $catagoryUrl);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_TIMEOUT,60);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'Accept:application/json',
            'Authorization: Bearer ' . $this->token
        ));
        $result = curl_exec($ch);
        curl_close($ch);
        if($result === false || !$result) {  
            return false;
        }
        $result = json_decode($result, TRUE);//return category array
        //print_r($result);
        return $result;
    }

/**
* 根据attributeSetId 获取类目  catalogAttributeSetRepositoryV1 GET /V1/products/attribute-sets/{attributeSetId}
* method： get
* return ： data
**/
    public function getattributeSetId($attributeSetId)
    {
        $catagoryUrl = "products/attribute-sets/$attributeSetId";
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $this->baseUrl . $catagoryUrl);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_TIMEOUT,60);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'Accept:application/json',
            'Authorization: Bearer ' . $this->token
        ));
        $result = curl_exec($ch);
        curl_close($ch);
        if($result === false || !$result) {  
            return false;
        }
        $result = json_decode($result, TRUE);//return category array
        //print_r($result);
        return $result;
    }

/**
* 根据 attributecode： catalogProductAttributeOptionManagementV1 GET /V1/products/attributes/{attributeCode}/options
* method： get
* return ： data
**/
    public function getproductAttributeCode($attribute_code)
    {
        $catagoryUrl = "products/attributes/$attribute_code/options";
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $this->baseUrl . $catagoryUrl);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'Accept:application/json',
            'Authorization: Bearer ' . $this->token
        ));
        $result = curl_exec($ch);
        curl_close($ch);
        $result = json_decode($result, TRUE);//return category array
        //print_r($result);
        return $result;
    }
/**
 * 超时处理
 */

public function timeoutoption($result){
    if($result === false || !$result) {  
            return false;
        }  
      
}
/**
* 订单接口 ： salesOrderRepositoryV1 GET /V1/orders
* method： get
* return ： data
*
*/
public function Byorderid($orderid){
        $catagoryUrl = "orders/$orderid";
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $this->baseUrl . $catagoryUrl);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'Accept:application/json',
            'Accept-Language:en-US,en;q=0.5',
            'Authorization: Bearer ' . $this->token
        ));
        $result = curl_exec($ch);
        curl_close($ch);
        $result = json_decode($result, TRUE);//return category array
        //print_r($result);
        return $result;

}
}
	
